package com.dzf.controller;

import com.dzf.Util.SessionUtil;
import com.dzf.entity.RespJson;
import com.dzf.entity.User;
import com.dzf.service.UserSerive;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserSerive userSerive;
    @Autowired
    SessionUtil sessionUtil;

    @RequestMapping("/go2MyInfo")
    public String go2UserInfo(){
        return "user/myInfo";
    }

    /**
     * 更新用户密码
     * @param user 用户信息
     * @return 请求是否成功
     */
    @RequestMapping("/updPwd")
    @ResponseBody
    public RespJson updPwd(@RequestBody User user)
    {
        User user1 = sessionUtil.getUser();
        user.setId(user1.getId());
        boolean b = userSerive.add(user);
        if (b)
        {
            return RespJson.success(200,"ok",b);
        }else{
            return RespJson.success(405,"修改失败！！！",b);
        }
    }

    /**
     * 更新用户名
     * @param user 用户信息
     * @return 请求是否成功
     */
    @RequestMapping("/updName")
    @ResponseBody
    public RespJson updName(@RequestBody User user)
    {
        User user1 = sessionUtil.getUser();
        user.setId(user1.getId());
        boolean b = userSerive.add(user);
        if (b)
        {
            return RespJson.success(200,"ok",b);
        }else{
            return RespJson.success(405,"修改失败！！！",b);
        }
    }

    /**
     * 获取用户对象
     * @return user对象
     */
    @RequestMapping("/getUser")
    @ResponseBody
    public RespJson getUser(){
        return RespJson.success(200,"ok",sessionUtil.getUser());
    }
}
